//
//  MainController.m
//  庐山通
//
//  Created by mac22 on 14/10/19.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "MainController.h"
#import "Grouble.h"
#import "MJRefresh.h"
#import "ZCJHomeCell.h"
#import "MyDataFrame.h"
#import "LeftSlideViewController.h"
#import "LeftSortsViewController.h"
#import "SelectFindHttp.h"
#import "TravelDecatil.h"
#import "TravelDecatilFrame.h"
#import "DecaticTableViewCell.h"

#define DecatilURL @"https://api.deyi.com/thread/view/?tid=5958033&page=1&perpage=20&ver=5&device=android&noimg=1&hash=2c214fd00c9de0a6b6a69369b1ed695a5cecdc2b"


@interface MainController()<MJRefreshBaseViewDelegate>
@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;

@property (nonatomic,strong)NSMutableArray * decaticFrame;


@end
@implementation MainController

- (NSMutableArray *)decaticFrame
{
    if (_decaticFrame == nil) {
        _decaticFrame = [[NSMutableArray alloc]init];
        
    }
    return _decaticFrame;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.设置导航栏的内容
    [self setupNavBar];
    [self setRush];
    
    
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.tableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.tableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    ZCJLog(@"jjloadMoreDatajjj");

}
- (void)loadNewData
{
    //[MBProgressHUD showMessage:@"正在加载"];
    [SelectFindHttp postWirhUrl:DecatilURL parms:nil success:^(id json) {
        
        // ZCJLog(@"%@",json);
        //NSArray * tempArray = ;
        NSArray * myDataFrame = [TravelDecatil objectArrayWithKeyValuesArray:json[@"posts"]];
        //[myDataFrame removeob]
        int i = 0;
        for (TravelDecatil * travel in myDataFrame) {
            if (i > 0) {
                TravelDecatilFrame * travleDataFrame = [[TravelDecatilFrame alloc]init];
                
                travleDataFrame.travelDecatl = travel;
                
                [self.decaticFrame addObject:travleDataFrame];
            }
            i ++;
        }
        [self.tableView reloadData];
       // [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"加载成功"];
        [self.head endRefreshing];
    } failture:^(id error) {
        [MBProgressHUD showError:@"加载失败"];
       // [MBProgressHUD hideHUD];
        [self.head endRefreshing];
    }];

}
- (void)dealloc
{
    [self.foot free];
    [self.head free];
}
- (void)setupNavBar
{
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_pop" highIcon:@"navigationbar_pop_highlighted" target:self action:@selector(pop)];
}

- (void)pop
{
   
    ZCJLog(@"地图导航");
}
#pragma mark- delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.decaticFrame.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DecaticTableViewCell * cell = [DecaticTableViewCell DecaticTableViewCell:tableView];
    TravelDecatilFrame * dataFrame = self.decaticFrame[indexPath.row];
    
    cell.myDataFrame = dataFrame;
    
    return cell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelDecatilFrame * cellHs = self.decaticFrame[indexPath.row];
  
    return cellHs.cellH;
}


@end
